const Koa = require('koa');
const Router = require('koa-router')
const cors = require('@koa/cors')
const {koaBody} = require('koa-body')
const json = require('koa-json')

const app = new Koa();
const router = new Router();

router.prefix('/api')

router.post('/user',async(ctx)=>{
    let {body} = ctx.request
    let header = ctx.header
    console.log(body)
    console.log(header.role)
    if (!body ||Object.keys(body).length === 0 || !body.name || !body.email) {
        ctx.body = {
            code:404,
            msg:"name与email不得为空"
        }
    } else if (!header.role || header.role != 'admin') {
        ctx.body = {
            code:401,
            msg:"unauthorized post"
        }
    } else {
        ctx.body = {
            code:200,
            data:body,
            msg:"上传成功"
        }
    }
})

app.use(koaBody())
app.use(cors())
app.use(router.routes())
app.use(json({pretty: false, param: 'pretty'}))
    .use(router.allowedMethods())
app.listen(3000)